package com.dhcc.sds.standard.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.sds.standard.dao.SdsStandDicMatchParamMapper;
import com.dhcc.sds.standard.entity.SdsStandDicEntity;
import com.dhcc.sds.standard.entity.SdsStandDicMatchParamEntity;
import com.dhcc.sds.standard.entity.SdsStandDicsubEntity;
import com.dhcc.sds.standard.entity.SdsStandSourceEntity;
import com.dhcc.sds.standard.service.ISdsStandDicMatchParamService;
import com.dhcc.sds.standard.service.ISdsStandDicService;
import com.dhcc.sds.standard.service.ISdsStandDicsubService;
import com.dhcc.sds.standard.service.ISdsStandSourceService;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * 项目标准化字典对照参数表数据服务层接口
 *
 * @ClassName: SdsStandDicMatchServiceImpl
 * @Description: TODO
 * @author zhangdc
 * @date 2022-03-03 10:09:57
 */
@Service("sdsStandDicMatchParamService")
public class SdsStandDicMatchParamServiceImpl extends BaseServiceImpl<SdsStandDicMatchParamMapper, SdsStandDicMatchParamEntity> implements ISdsStandDicMatchParamService {

    @Autowired
    private ISdsStandDicService sdsStandDicService;

    @Override
    public List<SdsStandDicMatchParamEntity> page(Page<SdsStandDicMatchParamEntity> page, Map<String, Object> params) {
        return this.baseMapper.page(page, params);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importMatch(List<List<Object>> indexAll) {
        for (int i = 1; i < indexAll.size(); i++){
            List<Object> ls = indexAll.get(i);
            String dicName = ls.get(0).toString();
            String keyInfo = "";
            if (ls.size()>1&&!StringUtils.isEmpty(ls.get(1))){
                keyInfo = ls.get(1).toString();
            }
            String genInfo = "";
            if (ls.size()>2 &&!StringUtils.isEmpty(ls.get(2))){
                genInfo = ls.get(2).toString();
            }
            String resume = "";
            if (ls.size()>3&&!StringUtils.isEmpty(ls.get(3))){
                resume = ls.get(3).toString();
            }
            if (CommonUtil.isEmpty(dicName)){
                continue;
            }
            //判断标准字典是否存在
            SdsStandDicEntity dicEntity = sdsStandDicService.selectOne(new EntityWrapper<SdsStandDicEntity>()
                    .eq("name", dicName));
            if (!CommonUtil.isEmpty(dicEntity)){
                UpdateMatch(dicEntity, keyInfo, genInfo, resume);
            }

        }
    }

    @Override
    public List<SdsStandDicMatchParamEntity> getDicBySource(List<String> sources) {
        return this.baseMapper.selectDicBySource(sources);
    }

    @Override
    public List<SdsStandDicMatchParamEntity> getDicsubBySource(List<String> sources) {
        return this.baseMapper.selectDicsubBySource(sources);
    }

    @Override
    public List<SdsStandDicMatchParamEntity> getBySource(List<String> sources) {
        return this.baseMapper.selectBySource(sources);
    }


    private void UpdateMatch(SdsStandDicEntity standDic, String keyInfo, String genInfo, String resume){
        if (!CommonUtil.isEmpty(keyInfo)){
            String keyInfo1 = keyInfo.replace("，", ",");
            String keyInfo2 = keyInfo1.replace("！", "!");
            String[] keys = keyInfo2.split(",");
            for (String key : keys){
                SdsStandDicMatchParamEntity sdsStandDicMatchParam = new SdsStandDicMatchParamEntity();
                sdsStandDicMatchParam.setDicId(standDic.getId());
                String filterType;
                if (key.contains("!")){
                    sdsStandDicMatchParam.setCompareContent(key.replace("!", ""));
                    filterType = "O";
                }else {
                    sdsStandDicMatchParam.setCompareContent(key);
                    filterType = "I";
                }
                SdsStandDicMatchParamEntity sdsStandDicMatchEntity1 = this.baseMapper.selectOne(sdsStandDicMatchParam);
                if (CommonUtil.isEmpty(sdsStandDicMatchEntity1)){
                    sdsStandDicMatchParam.setCompareType("K");
                    sdsStandDicMatchParam.setFilterType(filterType);
                    sdsStandDicMatchParam.setResume(resume);
                    sdsStandDicMatchParam.insert();
                }else {
                    sdsStandDicMatchEntity1.setCompareType("K");
                    sdsStandDicMatchEntity1.setFilterType(filterType);
                    sdsStandDicMatchEntity1.setResume(resume);
                    sdsStandDicMatchEntity1.updateAllColumnById();
                }
            }
        }
        if (!CommonUtil.isEmpty(genInfo)){
            String genInfo1 = genInfo.replace("，", ",");
            String genInfo2 = genInfo1.replace("！", "!");
            String[] gens = genInfo2.split(",");
            for (String gen : gens){
                SdsStandDicMatchParamEntity sdsStandDicMatchEntity = new SdsStandDicMatchParamEntity();
                sdsStandDicMatchEntity.setDicId(standDic.getId());
                String filterType;
                if (gen.contains("!")){
                    sdsStandDicMatchEntity.setCompareContent(gen.replace("!", ""));
                    filterType = "O";
                }else {
                    sdsStandDicMatchEntity.setCompareContent(gen);
                    filterType = "I";
                }

                SdsStandDicMatchParamEntity sdsStandDicMatchEntity1 = this.baseMapper.selectOne(sdsStandDicMatchEntity);
                if (CommonUtil.isEmpty(sdsStandDicMatchEntity1)){
                    sdsStandDicMatchEntity.setCompareType("C");
                    sdsStandDicMatchEntity.setFilterType(filterType);
                    sdsStandDicMatchEntity.setCompareContent(gen);
                    sdsStandDicMatchEntity.setResume(resume);
                    sdsStandDicMatchEntity.insert();
                }else {
                    sdsStandDicMatchEntity1.setCompareType("C");
                    sdsStandDicMatchEntity1.setFilterType(filterType);
                    sdsStandDicMatchEntity1.setResume(resume);
                    sdsStandDicMatchEntity1.updateAllColumnById();
                }
            }
        }
    }
}
